﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Pdm.Commons
{
    public class PdmOptimization : Optimization
    {
        public List<SpecificationInstance> urmWeights { get; set; }
        public List<SpecificationInstance> directions { get; set; }
        public List<SpecificationInstance> pdmWeights { get; set; }
        public double alpha { get; set; }
        public double profileDistance { get; set; }
        public double efficiency { get; set; }

        public PdmOptimization(DmuList dmuList, Dmu objectiveDmu)
            : base(dmuList, objectiveDmu)
        {
            this.urmWeights = new List<SpecificationInstance>();
            this.directions = new List<SpecificationInstance>();
            this.pdmWeights = new List<SpecificationInstance>();
        }
        public PdmOptimization(DmuList dmuList, Dmu objectiveDmu, double alpha) : this(dmuList, objectiveDmu)
        {
            this.alpha = alpha;
        }
        public PdmOptimization(DmuList dmuList, Dmu objectiveDmu, List<SpecificationInstance> urmWeights)
            : this(dmuList, objectiveDmu)
        {
            this.urmWeights = urmWeights;
        }
        public PdmOptimization(DmuList dmuList, Dmu objectiveDmu, List<SpecificationInstance> urmWeights, List<SpecificationInstance> directions, double alpha)
            : this(dmuList, objectiveDmu)
        {
            this.urmWeights = urmWeights;
            this.directions = directions;
            this.alpha = alpha;
        }
        public static List<SpecificationInstance> getOptimizationDirections(Optimization solvedDea, List<SpecificationInstance> urmWeights)
        {

            List<SpecificationInstance> directions = new List<SpecificationInstance>();

            int firstOut = solvedDea.dmuList.getIDofFirstOutputCoeffiecient();
            int i = 0;
            foreach (SpecificationInstance deaWeightSpec in solvedDea.deaWeights)
            {
                if (deaWeightSpec.spec.type == Type.Input)
                {
                    directions.Add(new SpecificationInstance(solvedDea.dmuList.coefficients[i], 0));
                }
                else
                {
                    if (deaWeightSpec.value / solvedDea.deaWeights[firstOut].value >
                       urmWeights[i].value / urmWeights[firstOut].value)
                    {
                        directions.Add(new SpecificationInstance(solvedDea.dmuList.coefficients[i], 1));
                    }
                    else
                    {
                        directions.Add(new SpecificationInstance(solvedDea.dmuList.coefficients[i], -1));
                    }
                }
                i++;
            }
            return directions;
        }
        public void setOptimizationDirections(Optimization solvedDea)
        {
            this.deaWeights = solvedDea.deaWeights;
            this.directions = PdmOptimization.getOptimizationDirections(solvedDea, this.urmWeights);
        }
    }
}
